package org.niko.ds.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.niko.ds.dao.ColumnDao;
import org.niko.ds.domain.DsColumn;
import org.springframework.transaction.annotation.Transactional;

public class ColumnDaoJpaImpl implements ColumnDao {
	protected final Logger logger = Logger.getLogger(getClass());

	@PersistenceContext
	private EntityManager entityManager;

	/*
	 * Add schema to the context and makes that instance managed (non-Javadoc)
	 * 
	 * @see org.niko.ds.dao.SchemaDao#saveSchema(org.niko.ds.domain.DsSchema)
	 */
	@Transactional
	public DsColumn save(DsColumn column) {
		entityManager.persist(column);
		logger.debug("Persisted schema with id=" + column.getId());
		return column;
	}

	public List<DsColumn> list() {
		Query query = entityManager.createQuery("SELECT c FROM DsColumn c");
		List<DsColumn> results = query.getResultList();
		logger.info("Found " + results.size() + " columns");
		return results;
	}
}